﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;

namespace CoolERP_DAL
{
    public interface ISKDDAL : IDAL<CW_SKD>
    {
        //List<SKDModel> GetSKD(Guid YSKZDID);
        List<SKDModel> QuerySKD(Dictionary<string, string> dc, string orderExpr, int? pageIndex, int pageSize, out int totalCount);

    }
    public class CW_SKDDAL : BaseDAL<CW_SKD>, ISKDDAL
    {
        public CW_SKDDAL(IUnitOfWork context)
            : base(context)
        { }
        //public List<SKDModel> GetSKD(Guid YSKZDID)
        //{
        //    var result = from skd in dataContext.CW_SKD.Where(t => t.YSKZDID == YSKZDID)
        //                 join users in dataContext.Users on skd.JBRID equals users.Id
        //                 join yskzd in dataContext.CW_YSKZD on skd.YSKZDID equals yskzd.ID
        //                 join xsht in dataContext.XSHT on yskzd.XSHTID equals xsht.ID
        //                 join kh in dataContext.ZL_KH on xsht.KHID equals kh.ID
        //                 join yhzh in dataContext.ZL_YHZH on skd.SRYHID equals yhzh.ID
        //                 select new SKDModel
        //                 {
        //                     ID = skd.ID,
        //                     BH = skd.BH,
        //                     YSKZDID = skd.YSKZDID,
        //                     YSKZDBH=yskzd.BH,
        //                     KHJC=kh.JIANCHENG,
        //                     SRYHID = skd.SRYHID,
        //                     SRYHMC=yhzh.YHMC,
        //                     ZPHM = skd.ZPHM,
        //                     DZRQ = skd.DZRQ,
        //                     SSJE = skd.SSJE,
        //                     JBRID = skd.JBRID,
        //                     JBRName = users.Name
        //                 };
        //    return result.ToList();
        //}
        //信用查询条件的查询收款单函数
        public List<SKDModel> QuerySKD(Dictionary<string, string> dc, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = from skd in dataContext.CW_SKD
                         join users in dataContext.Users on skd.JBRID equals users.Id
                         //join yskzd in dataContext.CW_YSKZD on skd.YSKZDID equals yskzd.ID
                         //join xsht in dataContext.XSHT on yskzd.XSHTID equals xsht.ID
                         //join kh in dataContext.ZL_KH on xsht.KHID equals kh.ID
                         join yhzh in dataContext.ZL_YHZH on skd.SRYHID equals yhzh.ID
                         select new SKDModel
                         {
                             ID = skd.ID,
                             BH = skd.BH,
                             //YSKZDID = skd.YSKZDID,
                             //YSKZDBH = yskzd.BH,
                             //KHJC = kh.JIANCHENG,
                             SRYHID = skd.SRYHID,
                             SRYHMC = yhzh.YHMC,
                             ZPHM = skd.ZPHM,
                             DZRQ = skd.DZRQ,
                             SSJE = skd.SSJE,
                             JBRID = skd.JBRID,
                             JBRName = users.Name
                         };
            if (dc != null && dc.Count > 0)
            {
                if (dc.ContainsKey("ID") && !string.IsNullOrEmpty(dc["ID"]))
                {
                    Guid temp = Guid.Parse(dc["ID"]);
                    result = result.Where(t => t.ID == temp);
                }
                if (dc.ContainsKey("BH") && !string.IsNullOrEmpty(dc["BH"]))
                {
                    string temp = dc["BH"];
                    result = result.Where(t => t.BH == temp);
                }
                //if (dc.ContainsKey("KHJC") && !string.IsNullOrEmpty(dc["KHJC"]))
                //{
                //    string temp = dc["KHJC"];
                //    result = result.Where(t => t.KHJC == temp);
                //}
                if (dc.ContainsKey("JBRName") && !string.IsNullOrEmpty(dc["JBRName"]))
                {
                    string temp = dc["JBRName"];
                    result = result.Where(t => t.JBRName == temp);
                }
                //if (dc.ContainsKey("YSKZDBH") && !string.IsNullOrEmpty(dc["YSKZDBH"]))
                //{
                //    string temp = dc["YSKZDBH"];
                //    result = result.Where(t => t.YSKZDBH == temp);
                //}
                if (dc.Keys.Contains("DZRQ") && !string.IsNullOrEmpty(dc["DZRQ"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["DZRQ"]).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    result = result.Where(t => t.DZRQ >= keyVal);
                }
                if (dc.ContainsKey("ZPHM") && !string.IsNullOrEmpty(dc["ZPHM"]))
                {
                    string temp = dc["ZPHM"];
                    result = result.Where(t => t.ZPHM == temp);
                }
            }
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
                return result.Skip(Convert.ToInt32(--pageIndex) * pageSize).Take(pageSize).ToList();

        }
    }
}
